from datetime import datetime, timedelta
from django.db import models
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from tagging.models import Tag
from tagging.fields import TagField
import clapton.utils.markdown as markdown
import clapton.utils.publisher as publisher

class Event(models.Model):
    site = models.ForeignKey(Site, verbose_name=_(u'Site'),) 
    user = models.ForeignKey(User, verbose_name=_(u'User'),)
    name = models.CharField(_(u'Name'), max_length=256,)
    slug = models.SlugField(_(u'Slug'), unique_for_date='pub_date',
	                    help_text=_(u'Automatically built from the name.'),)
    start_datetime = models.DateTimeField(_(u'Start time'), 
                                          help_text=_(u'Starting time of the event.'),)
    location = models.CharField(_(u'Location'), max_length=256, blank=True, null=True,)
    description = models.TextField(_(u'Description'), blank=True, null=True,
	                                    help_text=_(u'Use <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> syntax.'),)
    url = models.CharField(_(u'Website'), max_length=256, blank=True, null=True,
	                   help_text=_(u'URL of the event website.'),)
    tags = TagField(verbose_name=_(u'Tags'), blank=True, null=True,
                    help_text=_(u'Separate tags with spaces or commas.'))
    pub_date = models.DateTimeField(_(u'Date published'), 
		                    default=datetime.now,
                                    help_text=_(u'Publication date of the event.'),)
    publication_status = models.CharField(_(u'Publication Status'), 
	                                  max_length=32, choices=publisher.PUBLICATION_CHOICES,
					  default='Published',
					  help_text=_(u'Only published events will appear on the site. Drafts will only be visible on the Administration page.'),)
    
    objects = models.Manager()
    published_objects = publisher.PublishedManager()

    class Meta:
        verbose_name_plural = _(u'events')
	ordering = ['-start_datetime']
        get_latest_by = 'start_datetime'

    def __unicode__(self):
        return self.name
	
    def get_absolute_url(self):
        return '/events/%s/%s/' % (self.pub_date.strftime("%Y/%b/%d").lower(),
                                    self.slug)